home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / DynaLoader.pm < prev    next >
Text File  |  1995-03-03  |  8KB  |  242 lines

  1. package DynaLoader;
  2.  
  3. #
  4. #   And Gandalf said: 'Many folk like to know beforehand what is to
  5. #   be set on the table; but those who have laboured to prepare the
  6. #   feast like to keep their secret; for wonder makes the words of
  7. #   praise louder.'
  8. #
  9.  
  10. # Quote from Tolkien sugested by Anno Siegel.
  11. #
  12. # Read ext/DynaLoader/README and DynaLoader.doc for
  13. # detailed information.
  14. #
  15. # Tim.Bunce@ig.co.uk, August 1994
  16.  
  17. use Config;
  18. use Carp;
  19. use AutoLoader;
  20.  
  21. @ISA=(AutoLoader);
  22.  
  23.  
  24. # enable messages from DynaLoader perl code
  25. $dl_debug = 0 unless $dl_debug;
  26. $dl_debug = $ENV{'PERL_DL_DEBUG'} if $ENV{'PERL_DL_DEBUG'};
  27.  
  28. $dl_so = $dl_dlext = ""; # avoid typo warnings
  29. $dl_so = $Config{'so'}; # suffix for shared libraries
  30. $dl_dlext = $Config{'dlext'}; # suffix for dynamic modules
  31.  
  32. # Some systems need special handling to expand file specifications
  33. # (VMS support by Charles Bailey <bailey@HMIVAX.HUMGEN.UPENN.EDU>)
  34. # See dl_expandspec() for more details. Should be harmless but
  35. # inefficient to define on systems that don't need it.
  36. $do_expand = ($Config{'osname'} eq 'VMS');
  37.  
  38. @dl_require_symbols = ();       # names of symbols we need
  39. @dl_resolve_using   = ();       # names of files to link with
  40. @dl_library_path    = ();       # path to look for files
  41.  
  42. # This is a fix to support DLD's unfortunate desire to relink -lc
  43. @dl_resolve_using = dl_findfile('-lc') if $Config{'dlsrc'} eq "dl_dld.xs";
  44.  
  45. # Initialise @dl_library_path with the 'standard' library path
  46. # for this platform as determined by Configure
  47. push(@dl_library_path, split(' ',$Config{'libpth'}));
  48.  
  49. # Add to @dl_library_path any extra directories we can gather from
  50. # environment variables. So far LD_LIBRARY_PATH is the only known
  51. # variable used for this purpose. Others may be added later.
  52. push(@dl_library_path, split(/,/, $ENV{'LD_LIBRARY_PATH'}))
  53.     if $ENV{'LD_LIBRARY_PATH'};
  54.  
  55.  
  56. # No prizes for guessing why we don't say 'bootstrap DynaLoader;' here.
  57. &boot_DynaLoader if defined &boot_DynaLoader;
  58.  
  59. print STDERR "DynaLoader.pm loaded (@dl_library_path)¥n"
  60.     if ($dl_debug >= 2);
  61.  
  62. # Temporary interface checks for recent changes (Aug 1994)
  63. if (defined(&dl_load_file)){
  64. die "dl_error not defined" unless defined (&dl_error);
  65. die "dl_undef_symbols not defined" unless defined (&dl_undef_symbols);
  66. }
  67.  
  68. 1; # End of main code
  69.  
  70.  
  71. # The bootstrap function cannot be autoloaded (without complications)
  72. # so we define it here:
  73.  
  74. sub bootstrap {
  75.     # use local vars to enable $module.bs script to edit values
  76.     local(@args) = @_;
  77.     local($module) = $args[0];
  78.     local(@dirs, $file);
  79.  
  80.     croak "Usage: DynaLoader::bootstrap(module)"
  81.     unless ($module);
  82.  
  83.     croak "Can't load module $module, DynaLoader not linked into this perl"
  84.     unless defined(&dl_load_file);
  85.  
  86.     print STDERR "DynaLoader::bootstrap($module)¥n" if $dl_debug;
  87.  
  88.     my(@modparts) = split(/::/,$module);
  89.     my($modfname) = $modparts[-1];
  90.     my($modpname) = join(':',@modparts);
  91.     foreach (@INC) {
  92.         my $dir = $_;
  93.         $dir .= ":" unless $dir =~ /:$/;
  94.     $dir .= "auto:$modpname";
  95.     next unless -d $dir; # skip over uninteresting directories
  96.  
  97.     # check for common cases to avoid autoload of dl_findfile
  98.     last if ($file=_check_file("$dir:$modfname.$dl_dlext"));
  99.  
  100.     # no luck here, save dir for possible later dl_findfile search
  101.     push(@dirs, "-L$dir");
  102.     }
  103.     # last resort, let dl_findfile have a go in all known locations
  104.     $file = dl_findfile(@dirs, map("-L$_",@INC), $modfname) unless $file;
  105.  
  106.     croak "Can't find loadable object for module $module in ¥@INC"
  107.         unless $file;
  108.  
  109.     my($bootname) = "boot_$module";
  110.     $bootname =~ s/¥W/_/g;
  111.     @dl_require_symbols = ($bootname);
  112.  
  113.     # Execute optional '.bootstrap' perl script for this module.
  114.     # The .bs file can be used to configure @dl_resolve_using etc to
  115.     # match the needs of the individual module on this architecture.
  116.     my $bs = $file;
  117.     $bs =~ s/(¥.¥w+)?$/¥.bs/; # look for .bs 'beside' the library
  118.     if (-s $bs) { # only read file if it's not empty
  119.         local($osname, $dlsrc) = @Config{'osname','dlsrc'};
  120.         print STDERR "BS: $bs ($osname, $dlsrc)¥n" if $dl_debug;
  121.         eval { do $bs; };
  122.         warn "$bs: $@¥n" if $@;
  123.     }
  124.  
  125.     my $libref = DynaLoader::dl_load_file($file) or
  126.     croak "Can't load '$file' for module $module: ".&dl_error."¥n";
  127.  
  128.     my(@unresolved) = dl_undef_symbols();
  129.     carp "Undefined symbols present after loading $file: @unresolved¥n"
  130.         if (@unresolved);
  131.  
  132.     my $boot_symbol_ref = dl_find_symbol($libref, $bootname) or
  133.          croak "Can't find '$bootname' symbol in $file¥n";
  134.  
  135.     dl_install_xsub("${module}::bootstrap", $boot_symbol_ref, $file);
  136.     &{"${module}::bootstrap"}(@args);
  137. }
  138.  
  139.  
  140. sub _check_file{   # private utility to handle dl_expandspec vs -f tests
  141.     my($file) = @_;
  142.     return $file if (!$do_expand && -f $file); # the common case
  143.     return $file if ( $do_expand && ($file=dl_expandspec($file)));
  144.     return undef;
  145. }
  146.  
  147.  
  148. # Let autosplit and the autoloader deal with these functions:
  149. __END__
  150.  
  151.  
  152. sub dl_findfile {
  153.     # Read ext/DynaLoader/DynaLoader.doc for detailed information.
  154.     # This function does not automatically consider the architecture
  155.     # or the perl library auto directories.
  156.     my (@args) = @_;
  157.     my (@dirs,  $dir);   # which directories to search
  158.     my (@found);         # full paths to real files we have found
  159.     my ($vms) = ($Config{'osname'} eq 'VMS');
  160.  
  161.     print STDERR "dl_findfile(@args)¥n" if $dl_debug;
  162.  
  163.     # accumulate directories but process files as they appear
  164.     arg: foreach(@args) {
  165.         #  Special fast case: full filepath requires no search
  166.         if (/.:/ && -f $_ && !$do_expand){
  167.         push(@found,$_);
  168.         last arg unless wantarray;
  169.         next;
  170.     }
  171.  
  172.         # Deal with directories first:
  173.         #  Using a -L prefix is the preferred option (faster and more robust)
  174.         if (m:^-L:){ s/^-L//; push(@dirs, $_); next; }
  175.         #  Otherwise we try to try to spot directories by a heuristic
  176.         #  (this is a more complicated issue than it first appears)
  177.         if (m/.:/ && -d $_){   push(@dirs, $_); next; }
  178.  
  179.         #  Only files should get this far...
  180.         my(@names, $name);    # what filenames to look for
  181.         if (m:-l: ){          # convert -lname to appropriate library name
  182.             s/-l//;
  183.             push(@names,"lib$_.$dl_so");
  184.             push(@names,"lib$_.a");
  185.         }else{                # Umm, a bare name. Try various alternatives:
  186.             # these should be ordered with the most likely first
  187.             push(@names,"$_.$dl_so")     unless m/¥.$dl_so$/o;
  188.             push(@names,"lib$_.$dl_so")  unless m/.:/;
  189.             push(@names,"$_.o")          unless m/¥.(o|$dl_so)$/o;
  190.             push(@names,"$_.a")          unless m/¥.a$/;
  191.             push(@names, $_);
  192.         }
  193.         foreach $dir (@dirs, @dl_library_path) {
  194.             next unless -d $dir;
  195.             foreach $name (@names) {
  196.         my($file) = "$dir:$name";
  197.                 print STDERR " checking in $dir for $name¥n" if $dl_debug;
  198.         $file = _check_file($file);
  199.         if ($file){
  200.                     push(@found, $file);
  201.                     next arg; # no need to look any further
  202.                 }
  203.             }
  204.         }
  205.     }
  206.     if ($dl_debug) {
  207.         foreach(@dirs) {
  208.             print STDERR " dl_findfile ignored non-existent directory: $_¥n" unless -d $_;
  209.         }
  210.         print STDERR "dl_findfile found: @found¥n";
  211.     }
  212.     return $found[0] unless wantarray;
  213.     @found;
  214. }
  215.  
  216.  
  217. sub dl_expandspec{
  218.     my($spec) = @_;
  219.     # Optional function invoked if DynaLoader.pm sets $do_expand.
  220.     # Most systems do not require or use this function.
  221.     # Some systems may implement it in the dl_*.xs file in which case
  222.     # this autoload version will not be called but is harmless.
  223.  
  224.     # This function is designed to deal with systems which treat some
  225.     # 'filenames' in a special way. For example VMS 'Logical Names'
  226.     # (something like unix environment variables - but different).
  227.     # This function should recognise such names and expand them into
  228.     # full file paths.
  229.     # Must return undef if $spec is invalid or file does not exist.
  230.  
  231.     my($file)   = $spec; # default output to input
  232.     my($osname) = $Config{'osname'};
  233.  
  234.     if ($osname eq 'VMS'){ # dl_expandspec should be defined in dl_vms.xs
  235.     croak "dl_expandspec: should be defined in XS file!¥n";
  236.     }else{
  237.     return undef unless -f $file;
  238.     }
  239.     print STDERR "dl_expandspec($spec) => $file¥n" if $dl_debug;
  240.     $file;
  241. }
  242.